package com.volleyrequset;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private RequestQueue requestQueue;
    private Button btnGet;
    private Button btnPost;
    private Button btnJSON;
    private EditText edtPhone;
    private TextView txtResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindView();

        requestQueue = Volley.newRequestQueue(MainActivity.this);

        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = edtPhone.getText().toString();
                useVolleyStringRequestGet(phone);
            }
        });

        btnPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = edtPhone.getText().toString();
                useVolleyStringRequestPost(phone);
            }
        });

        btnJSON.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useVolleyJSONRequest();
            }
        });
    }

    private void useVolleyJSONRequest() {
        String url = "http://t.weather.sojson.com/api/weather/city/101280101";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG, jsonObject.toString());
                try {
                    StringBuffer stringBuffer = new StringBuffer();
                    for (int i = 0; i < 3; i++) {
                        JSONObject forecast = jsonObject.getJSONObject("data").getJSONArray("forecast").getJSONObject(i);
                        String date = forecast.getString("date");
                        String type = forecast.getString("type");
                        String week = forecast.getString("week");
                        stringBuffer.append(" 日期:" + date + "号   ").append("天气:" + type).append("  " + week + "\n");
                    }
                    txtResult.setText(stringBuffer.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    private void useVolleyStringRequestPost(final String phone) {
        String phoneUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, phoneUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG, s);
                txtResult.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("mobileCode", phone);
                map.put("userID", "");
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    private void useVolleyStringRequestGet(String phone) {
        String phoneUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String url = phoneUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG, s);
                txtResult.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        });
        requestQueue.add(stringRequest);
    }

    //绑定组件
    private void bindView() {
        btnGet = findViewById(R.id.btn_get);
        btnPost = findViewById(R.id.btn_post);
        edtPhone = findViewById(R.id.edt_phone);
        btnJSON = findViewById(R.id.btn_JSON);
        txtResult = findViewById(R.id.txt_result);
    }

}
